﻿using Awsc.DataAcquisitionSys.Core.Common.Expressions;
using Awsc.DataAcquisitionSys.Core.DataAcquisition;
using Awsc.DataAcquisitionSys.Core.Model;
using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Service.Alarm.Implement;
using Awsc.DataAcquisitionSys.Core.Service.Alarm.Interfaces;
using Awsc.DataAcquisitionSys.Core.Services.ConfigManagement;
using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition;
using Microsoft.Extensions.Options;
using Moq;
using System.Diagnostics;

class Program
{
    public static async Task Main(string[] args)
    {


        await DataCollectorTest();
        await ExpressionCalculatorTest();
        Thread.Sleep(1000000);
        Console.ReadLine();
    }

    public static async Task ExpressionCalculatorTest()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator("(环形防喷器压力>10&&悬重<20)||流动防顶卡瓦状态!=TRUE");
        Console.WriteLine(string.Join(",", expressionEvaluator.Fields));
        for (var i = 0; i < 10000; i++)
        {
            expressionEvaluator.SetVariable("环形防喷器压力", 100);
            expressionEvaluator.SetVariable("悬重", 100);
            expressionEvaluator.SetVariable("流动防顶卡瓦状态", false);

            var result = expressionEvaluator.CompileExpression()();

        }
        sw.Stop();
        Console.WriteLine(sw.ElapsedMilliseconds);




    }
    public static async Task DataCollectorTest()
    {
        DeviceConfig deviceConfig = new DeviceConfig();
        List<CollectorConfig> configs = new List<CollectorConfig>();
        CollectorConfig config = new CollectorConfig()
        {
            Name = "test",
            Model = "MT1333",
            SlaveAddress = 1,
            IP = "127.0.0.1",
            Port = 502,
            AICount = 1,
            AOCount = 0,
            DICount = 1,
            DOCount = 0,
            UpdateFreq = 1000,
            InputRegisters = new List<RegisterConfig>() {
                new RegisterConfig()
                {
                    Name = "套压",
                    CollectorName = "test",
                    CollectorChannelId = 1,
                    OutputMin = 0,
                    OutputMax = 100,
                    RangeMin = 0,
                    RangeMax = 100,
                    RangeUnit = "MPa",
                    WarnMin = 0,
                    WarnMax=70,
                    KValue=1,
                    CreatAICoil = true,
                    OpenValue = 10,
                    CloseValue = 0
                }
            },
            InputCoils = new List<CoilConfig>() {
                new CoilConfig(){
                    Name = "上闸板",
                    CollectorName = "test",
                    CoilType = CoilType.DI,
                    //BeReverse = true,
                    CollectorChannelId=1
                }
            }
        };
        configs.Add(config);
        deviceConfig.Collectors = configs;
       var mockObj =  new Mock<IDeviceConfigService>();
        mockObj.Setup(m => m.GetDeviceConfig()).Returns(deviceConfig);

        var mockServiceProvider = new Mock<IServiceProvider>();
        mockServiceProvider.Setup(m => m.GetService(typeof(IDeviceConfigService))).Returns(mockObj.Object);

        DataCollectEngine dataCollectorManager = new DataCollectEngine(mockServiceProvider.Object);
        dataCollectorManager.DataRecieved += (sender, e) =>
        {
            var entry = e.GetSensor("套压");
            var entryCoil = e.GetSensor("上闸板");
            Console.WriteLine($"采集到数据：{entry.Timestamp}--{entry.Name}: {entry.CurrentValue}, {entryCoil.Timestamp}--{entryCoil.Name}: {entryCoil.CurrentValue}");
        };
        await dataCollectorManager.StartAsync();

    }
    public static async Task AlarmTest()
    {
        var alarmRule = new ConditionAlarmRule();
        alarmRule.Condition = "Temperature>100 || pressure>100";
        alarmRule.Name = "TestRule";
        alarmRule.AlarmLevel = AlarmLevel.Warning;

        GlobalDeviceManager table = GlobalDeviceManager.Instance;

        AlarmEngine engine = new AlarmEngine();


        Random r = new Random();


        Task.Run(async () =>
        {
            while (true)
            {
                table.SetValue("Temperature", r.Next(50, 150));
                table.SetValue("pressure", r.Next(50, 150));
                await Task.Delay(1000);

            }

        });

        Task.Run(async () =>
        {
            await engine.StartAsync(new List<IAlarmRule>() { alarmRule });
        });
        Thread.Sleep(1000000);
        Console.ReadLine();
    }
}
